package prototcp

import (
	"encoding/hex"
	"errors"
)

const (
	OUTPUT_HEX_DUMP   = "HEX-DUMP"
	OUTPUT_TEXT       = "TEXT"
	OUTPUT_HEX_STREAM = "HEX-STREAM"
)

var _hexTcpOutput = &hexTcpOutput{}
var _textTcpOutput = &textTcpOutput{}
var _hexStreamTcpOutput = &hexStreamTcpOutput{}

type ITcpOutput interface {
	ToString(data []byte) string
}

func GetTcpOutput(output string) (ITcpOutput, error) {
	if output == OUTPUT_HEX_DUMP {
		return _hexTcpOutput, nil
	}
	if output == OUTPUT_TEXT {
		return _textTcpOutput, nil
	}
	if output == OUTPUT_HEX_STREAM {
		return _hexStreamTcpOutput, nil
	}
	return nil, errors.New("unsupported output")
}

type hexTcpOutput struct {
}

func (h *hexTcpOutput) ToString(data []byte) string {
	return hex.Dump(data)
}

type textTcpOutput struct {
}

func (h *textTcpOutput) ToString(data []byte) string {
	return string(data)
}

type hexStreamTcpOutput struct {
}

func (h *hexStreamTcpOutput) ToString(data []byte) string {
	return hex.EncodeToString(data)
}
